﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DjBlog.Blog
{
    public partial class Default : System.Web.UI.Page
    {
        protected int pageindex;
        protected int pagesize = 10;
        protected IList<DjModel.PostInfo> postlist = null;
        protected string nextpage = string.Empty;
        protected string prepage = string.Empty;

        protected string title = string.Empty;
        protected string description = string.Empty;
        protected string keyword = string.Empty;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }

        private void Bind()
        {
            string url = "/blog/";
            DjModel.PostSearchInfo postsearch = new DjModel.PostSearchInfo();
            int catid = DjCommon.Utils.GetInt("catid");
            if (catid > 0)
            {
                string s = DjBll.Term.GetCatPost(catid);
                if (!string.IsNullOrEmpty(s))
                {
                    postsearch.PostID = s;
                }
                url = "/cat/" + catid;

                if (DjBll.Term.TermCateDic.ContainsKey(catid))
                {
                    title = DjBll.Term.TermCateDic[catid].TermName;
                    description = title;
                    keyword = title;
                }

            }
            int tagid = DjCommon.Utils.GetInt("tagid");
            if (tagid > 0)
            {
                string s = DjBll.Term.GetCatPost(tagid);
                if (!string.IsNullOrEmpty(s))
                {
                    postsearch.PostID = s;
                }
                url = "/tag/" + tagid;
                if (DjBll.Term.TermTagDic.ContainsKey(tagid))
                {
                    title = DjBll.Term.TermTagDic[tagid].TermName;
                    description = title;
                    keyword = title;
                }
            }
            string key = DjCommon.Utils.GetQueryString("key");
            if (!string.IsNullOrEmpty(key))
            {
                postsearch.Title = HttpContext.Current.Server.UrlDecode(key);
                url = "/blog/" + key;

                title = "Search for " + HttpContext.Current.Server.UrlDecode(key);
                description = title;
                keyword = title;
            }
            pageindex = DjCommon.Utils.GetInt("page", 1);
            postlist = DjBll.Post.GetPostList(pageindex, pagesize, postsearch);
            if (pageindex > 1)
            {
                prepage = url + "_" + (pageindex - 1) + ".html";
            }
            if (postlist!=null&&postlist.Count == pagesize)
            {
                nextpage = url + "_" + (pageindex + 1) + ".html";
            }

        }
    }
}
